package com.hwz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hwz.constant.MessageConstant;
import com.hwz.constant.StatusConstant;
import com.hwz.dto.SetmealDTO;
import com.hwz.dto.SetmealPageQueryDTO;
import com.hwz.entity.Dish;
import com.hwz.entity.Setmeal;
import com.hwz.entity.SetmealDish;
import com.hwz.exception.DeletionNotAllowedException;
import com.hwz.exception.SetmealEnableFailedException;
import com.hwz.mapper.DishMapper;
import com.hwz.mapper.SetmealDishMapper;
import com.hwz.mapper.SetmealMapper;
import com.hwz.result.PageResult;
import com.hwz.service.SetmealService;
import com.hwz.vo.DishItemVO;
import com.hwz.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/16 15:57]
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && setmealDishes.size() > 0)
        {
            for (SetmealDish setmealDish : setmealDishes)
            {
                setmealDish.setSetmealId(setmealId);
                setmealDishMapper.insertSetmealDish(setmealDish);
            }
        }
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealPage = setmealMapper.selectByCondition(setmealPageQueryDTO);
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id: ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        ids.forEach(id ->{
            setmealMapper.deleteById(id);
            setmealDishMapper.deleteBySetmealId(id);
        });
    }

    @Override
    public Setmeal getById(Long id) {
        //查询套餐基本信息
        Setmeal setmeal = setmealMapper.getById(id);
        return setmeal;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish ->{
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.insertSetmealDish(setmealDish);
        });
    }

    @Override
    public void startOrOff(Long id, Integer status) {
        if (status == StatusConstant.ENABLE)
        {
            //检查里面的菜品是否起售
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
            setmealDishes.forEach(setmealDish ->{
                Long dishId = setmealDish.getDishId();
                Dish dish = dishMapper.selectById(dishId);
                if (dish.getStatus() == StatusConstant.DISABLE)
                {
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐数据
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.getByCategoryId(categoryId);
        return setmeals;
    }

    @Override
    public List<DishItemVO> getDishItemVOS(Long setmealId) {
        List<DishItemVO> dishItemVOS = setmealDishMapper.getDishItemVOS(setmealId);
        return dishItemVOS;
    }
}
